import json
import os.path

json_file= '../config/parameters.json'

json_file_backup={
    "attack_method": "CPA",
    "target_algorithm": "AES",
    "leakage_model": "Enc_SboxOut_MClass",
    "target_byte": 2,
    "raw_data_path": "../raw_dataset/rawdata.trs",
    "trs_key_status": "fixed",
    "fixed_key": ["01", "23", "45", "67", "89", "ab", "cd", "ef", "fe", "dc", "ba", "98", "76", "54", "32", "10"],
    "trs_key_range": [16, 32],
    "trs_plaintext_status": "random",
    "trs_plaintext_range": [0, 16],
    "trs_ciphertext_status": "unknow",
    "trs_ciphertext_range": [0, 16],
    "trs_profile_range": [0, 50000],
    "profile_file_dir": "../profile_dataset/",
    "trs_analysis_range": [50000, 60000],
    "analysis_file_dir": "../analysis_dataset/",
    "model_file_dir": "../model/",
    "auto_save_result": True,
    "result_file_dir": "../result/",
    "DLSCA_network": "MLP2",
    "epochs": 200,
    "batch_size": 200,
    "learning_rate": 0.0001,
    "activation": "relu",
    "validation_split": 0.01,
    "attack_steps": 10
}

def check():
    if os.path.exists(json_file) is False:
        with open(json_file, 'w') as f:
            json.dump(json_file_backup, f, indent=4)

def json_write(dict):
    check()
    with open(json_file,'r') as f:
        data=json.load(f)
        for key in dict:
            data[key]=dict[key]
    with open(json_file, "w") as f:
        json.dump(data, f,indent=4)

def json_read(key=None):
    check()
    with open(json_file,'r') as f:
        data=json.load(f)
    if key in data:
        return data[key]
    return data